package com.eyes.ds.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.eyes.ds.R;

import java.util.List;
import java.util.Objects;

/**
 * Author Jayy.Lucas
 * Email: eyes.left@qq.com
 * Date: 2017/11/4
 * Time: 下午3:50
 */
public class PhotoAdapter extends ArrayAdapter {

    private int resource;
    private List<Integer> photos;
    private Context context;
    private Integer pos = -1;
    private Integer lastPos = -2;
    private int lastColor = Color.argb(255,0,0,0);      //transparent

    public PhotoAdapter(Context context, int resource, List<Integer> photos) {
        super(context, resource, photos);
        this.context = context;
        this.resource = resource;
        this.photos = photos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(resource, parent, false);
        ImageView iv = ViewHolder.get(convertView, R.id.iv_photo);
        iv.setImageResource(photos.get(position));
        if (pos != null && pos == position) {
            lastColor = iv.getDrawingCacheBackgroundColor();
            iv.setBackgroundColor(Color.parseColor("#337AB7"));
            if(!Objects.equals(lastPos, pos)){
                lastPos = pos;
            }
        } else {
            iv.setBackgroundColor(lastColor);
        }
        return convertView;
    }

    /**
     * 这个方法在每次点击的时候都只执行一次
     * @param pos 当前选择的蔬菜的position
     */
    public void setPos(Integer pos) {
        this.pos = pos;
    }

    public void setLastPos(Integer pos){
        this.lastPos = pos;
    }

    public Integer getPos() {
        return pos;
    }

    public Integer getLastPos() {
        return lastPos;
    }

    public List<Integer> getPhotos() {
        return photos;
    }
}
